import sys
from PySide6.QtWidgets import QWidget, QApplication, QLineEdit, QMainWindow, QTextBrowser


class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        # 窗口标题
        self.setWindowTitle('拖拽获取文件路径')
        # 定义窗口大小
        self.resize(500, 400)
        self.QLineEdit = QLineEdit(self)
        self.QLineEdit.resize(300, 50)
        self.QLineEdit.setGeometry(25, 100, 450, 50)
        # 调用Drops方法
        self.setAcceptDrops(True)

    # 鼠标拖入事件
    def dragEnterEvent(self, evn):
        self.setWindowTitle('鼠标拖入窗口了')
        self.QLineEdit.setText(evn.mimeData().urls()[0].toLocalFile())
        # 鼠标放开函数事件
        evn.accept()

    # 鼠标放开执行
    def dropEvent(self, evn):
        self.setWindowTitle('鼠标放开了')

    # 鼠标移入窗口
    def dragMoveEvent(self, evn):
        self.setWindowTitle('鼠标移入')


class Window(QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.paths = ""
        self.setWindowTitle('文件拖入')  # ==> 窗口标题
        self.resize(500, 400)  # ==> 定义窗口大小
        self.textBrowser = QTextBrowser()
        self.setCentralWidget(self.textBrowser)
        self.setAcceptDrops(True)  # ==> 必须设置

    def dragEnterEvent(self, event):
        file = event.mimeData().urls()[0].toLocalFile()
        if file not in self.paths:  # ==> 去重显示
            self.paths += file + "\n"
            print("拖拽的文件 ==> {}".format(file))
            self.textBrowser.setText(self.paths)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec())
